package com.laolei.template.test;

import java.io.IOException;
import java.io.PrintWriter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import com.laolei.template.assist.BuildData;
import com.laolei.template.assist.OutData;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

@Component
public class FreeMarkerTemplateBuild implements CommandLineRunner{
	@Autowired FreeMarkerViewResolver freeMarkerViewResolver;
	@Autowired Configuration configuration;

	@Override
	public void run(String... args) throws Exception {
		this.test();
		this.testResolver();
	}

	private void test() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, TemplateException, IOException {
		configuration
		.getTemplate("abc.ftl")
		.process(BuildData.build(), new PrintWriter(System.err));
		System.err.println();
	}
	
	private void testResolver() throws Exception {
		View view=freeMarkerViewResolver.resolveViewName("abc",null);
		OutData.out(view);
	}

}
